package com.surveypark.struts2.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.surveypark.domain.Answer;
import com.surveypark.domain.Question;
import com.surveypark.service.SurveyService;

@Controller("collectionSurveyAction")
@Scope("prototype")
public class CollectionSurveyAction extends BaseAction<Question> {
	
	private static final long serialVersionUID = 2676947663830369209L;
	
	private Integer sid;
	@Resource(name="surveyService")
	private SurveyService surveyService;
	private String fileName;
	
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public InputStream getIs(){
		this.fileName = UUID.randomUUID().toString();
		try{
			HSSFWorkbook wb = new HSSFWorkbook();
			HSSFSheet sheet = wb.createSheet("surveypark sheet");
			HSSFRow row = sheet.createRow(0);
			HSSFCell cell = null;

			List<Question> questions = surveyService.getQuestions(sid);
			
			HSSFCellStyle style = wb.createCellStyle();
			style.setWrapText(true);//���ñ���е�Ԫ�����ı�
			
			Question q = null;
			Map<Integer, Integer> qidIndexMap = new HashMap<Integer, Integer>();
			for(int i = 0; i < questions.size(); i++){
				q = questions.get(i);
				cell = row.createCell(i);
				cell.setCellValue(q.getTitle());
				cell.setCellStyle(style);
				sheet.setColumnWidth(i, 8000);
				qidIndexMap.put(q.getId(), i);
			}
			String oldUuid = "";
			String newUuid = "";
			int rowIndex = 0;
			
			//分库。。。检测
			
			List<Answer> answers = surveyService.findAnswers(sid);
			for(Answer a:answers){
				newUuid = a.getUuid();
				if(!oldUuid.equals(newUuid)){
					oldUuid = newUuid;
					row = sheet.createRow(++rowIndex);//Ϊÿ�ε��鴴���µ�һ��
				}
				cell = row.createCell(qidIndexMap.get(a.getQuestionId()));
				cell.setCellValue(a.getAnswerIds());
				cell.setCellStyle(style);
			}
			ByteArrayOutputStream os = new ByteArrayOutputStream();//�Ƚ����д���ڴ��У�Ȼ���ٴ��ڴ��л�ȡ��input��
			wb.write(os);
			wb.close();
			return new ByteArrayInputStream(os.toByteArray());//����ת�����ֽ������д�뵽��������;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}



	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}

	public SurveyService getSurveyService() {
		return surveyService;
	}

	public void setSurveyService(SurveyService surveyService) {
		this.surveyService = surveyService;
	}

}
